package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.File;
import com.example.service.FileService;
import org.apache.commons.io.IOUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;

@RestController
public class FileController {



    @Resource
    FileService service;


    @RequestMapping(value = "/showpdf/{sid}")
    public void showpdf(@PathVariable String sid, HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            LambdaQueryWrapper<File> lqw = new LambdaQueryWrapper<>();
            lqw.eq(sid!=null, File::getStuId, sid);
            File one = service.getOne(lqw);
            java.io.File file = new java.io.File(one.getDateDirPath()+"/"+one.getFileName());
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Type", "application/pdf");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
